package com.tobby.camel.camelinaction.chapter3.order;

import java.math.BigDecimal;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.dataformat.BindyType;
import org.junit.Test;

public class PurchaseOrderBindyTest {

	@Test
	public void testBindy() throws Exception {
		CamelContext context = new DefaultCamelContext();
		context.addRoutes(createRoute());
		context.start();
		MockEndpoint endpoint = context.getEndpoint("mock:result", MockEndpoint.class);
		endpoint.expectedBodiesReceived("Camel in Action,49.95,1\n");

		BindyOrder order = new BindyOrder();
		order.setAmount(1);
		order.setPrice(new BigDecimal("49.95"));
		order.setName("Camel in Action");

		ProducerTemplate template = context.createProducerTemplate();
		template.sendBody("direct:toCsv", order);
		Thread.sleep(2000);
		endpoint.assertIsSatisfied();
	}

	private RouteBuilder createRoute() {
		return new RouteBuilder() {

			@Override
			public void configure() throws Exception {
				from("direct:toCsv").marshal().bindy(BindyType.Csv, BindyOrder.class)
						.to("mock:result");
			}
		};
	}
}
